package com.xiaowei.gym.gymjsp.ware.service.impl;

import com.xiaowei.gym.gymjsp.ware.dao.GymMenuDao;
import com.xiaowei.gym.gymjsp.ware.dao.impl.GymMenuDaoImpl;
import com.xiaowei.gym.gymjsp.ware.entity.GymMenuEntity;
import com.xiaowei.gym.gymjsp.ware.service.GymMenuService;

import java.util.List;
import java.util.stream.Collectors;

public class GymMenuServiceImpl implements GymMenuService {

    private GymMenuDao gymMenuDao;

    public GymMenuServiceImpl() {
        gymMenuDao=new GymMenuDaoImpl();
    }

    @Override
    public List<GymMenuEntity> getAllMenus() {
        List<GymMenuEntity> menuList = gymMenuDao.getAllMenus();
        // 将菜单项列表转换为以id为键的map，方便通过id查找菜单项
        List<GymMenuEntity> menuEntities = menuList.stream().filter((item) -> {
            return item.getParentId() == 0;
        }).map((menu) -> {
            System.out.println("一级菜单:"+menu);
            menu.setChildren(buildMenuTreeRecursive(menu, menuList));
            return menu;
        }).collect(Collectors.toList());
        return menuEntities;
    }

    @Override
    public GymMenuEntity getMenuById(int id) {
        return null;
    }

    @Override
    public void addMenu(GymMenuEntity menu) {

    }

    @Override
    public void updateMenu(GymMenuEntity menu) {

    }

    @Override
    public void deleteMenu(int id) {

    }

    private List<GymMenuEntity> buildMenuTreeRecursive(GymMenuEntity menu, List<GymMenuEntity> menuAll) {
        List<GymMenuEntity> gymMenuEntityList = menuAll.stream().filter((item) -> {
            return item.getParentId().equals(menu.getId());
        }).map(menuEntity -> {
            menuEntity.setChildren(buildMenuTreeRecursive(menuEntity, menuAll));
            return menuEntity;
        }).collect(Collectors.toList());
        return gymMenuEntityList;
    }
}
